﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace first_project
{
    class Asteroid
    {
        public const int MAX_LIFE = 10;

        public const float MIN_SPEED = 1;
        /// <summary>
        /// Image for the class Asteroid
        /// </summary>
        private Texture2D image;
        /// <summary>
        /// Position for the class Asteroid
        /// </summary>
        private Vector2 position;
        /// <summary>
        /// Position for the class Asteroid
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        
        public Rectangle CollisionRect
        {
            get { return new Rectangle((int)position.X,(int)position.Y, this.image.Width,this.image.Height); }
        }

        /// <summary>
        /// Direction for the class Asteroid
        /// </summary>
        private Vector2 direction;

        private Vector2 speed;

        public Vector2 Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        private int life;

        public int Life
        {
            get { return life; }
            set { life = value; }
        }

        private GameWindow Window;

        private float scale;

        private Random random;

        public Asteroid(Texture2D image, int life, GameWindow Window)
        {
            this.image = image;
            this.life = life;
            this.Window = Window;
            this.random = new Random();
            this.position = new Vector2(Window.ClientBounds.Width * (float) random.NextDouble(), Window.ClientBounds.Height * (float) random.NextDouble());

            this.direction = new Vector2 (MathHelper.ToRadians(random.Next(-360,360)),MathHelper.ToRadians(random.Next(-360,360)));
            this.scale = (float)life / MAX_LIFE;

            this.speed = new Vector2((MIN_SPEED / scale), (MIN_SPEED / scale));

            /*
             * TODO:
             * 1 Criar uma lista de asteroids.
             * 2 ajustar a velocidade em relacao a escalha
             */
        }

        public void update(GameTime gameTime)
        {
            this.position += direction * speed;

            #region Retorno na tela(Returns object to other side of screen)

            if (Position.X >= Window.ClientBounds.Width + image.Width / 2)
            {
                position.X = -image.Width / 2;
            }
            else if (Position.X <= -image.Width / 2)
            {
                position.X = Window.ClientBounds.Width + image.Width / 2;
            }
            else if (Position.Y >= Window.ClientBounds.Height + image.Height / 2)
            {
                position.Y = -image.Height / 2;
            }
            else if (Position.Y <= -image.Height / 2)
            {

                position.Y = Window.ClientBounds.Height + image.Height / 2;
            }
            #endregion
        }

        public void draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(image, position, null, Color.White, 0.0f, new Vector2(image.Width / 2, image.Height / 2), scale, SpriteEffects.None, 0);
        }
    }
}
